package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "后台商品管理控制器")
@RestController //@Controller{控制器}+@ResponseBody{返回数据变为Json}
@RequestMapping("/admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    @Autowired
    private SpuImageService spuImageService;

    /**
     * 根据三级分类Id 查询spu列表
     * admin/product/1/10 restful 传值风格
     * ?category3Id=61 普通传值，url拼接传值: request.getParameter("category3Id"); ; @RequestParam ; 对象传值
     * http://localhost/admin/product/1/10?category3Id=61
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuList(@PathVariable Long page,
                             @PathVariable Long limit,
                             SpuInfo spuInfo,
                             HttpServletRequest request){
        //  构建Page 对象，传递page,limit
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        //  调用服务层方法.
        IPage<SpuInfo> iPage = this.manageService.getSpuList(spuInfoPage,spuInfo.getCategory3Id());
        //  返回分页数据。
        return Result.ok(iPage);
    }

    /**
     * 获取所有销售属性数据
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        //  调用服务层方法.
        List<BaseSaleAttr> baseSaleAttrList = this.manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    /**
     * 保存spuInfo
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //  调用服务层方法.
        this.manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据spuId 获取集合列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //  调用服务层方法
        //  List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        List<SpuImage> spuImageList = spuImageService.list(new LambdaQueryWrapper<SpuImage>().eq(SpuImage::getSpuId, spuId).orderByDesc(SpuImage::getId));
        return Result.ok(spuImageList);
    }


    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        //  调用服务层方法 集合的泛型
        List<SpuSaleAttr> spuSaleAttrList = this.manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}
